package com.qianya.system.manage.web.login.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.qianya.model.common.ResultCode;
import com.qianya.system.manage.web.login.model.LoginUsersPermission;
import com.qianya.system.manage.web.login.service.ILoginUsersPermissionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.qianya.model.common.CommonResult;
import com.qianya.model.pojo.login.qo.LoginUsersPermissionQO;
import com.qianya.model.pojo.login.vo.LoginUsersPermissionVO;
import javax.servlet.http.HttpServletRequest;
    import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author lhl
* @since 2020-07-02
*/
    @RestController
@RequestMapping("/login-users-permission")
@Api(value = "LoginUsersPermissionController", tags = "菜单接口")
@Slf4j
public class LoginUsersPermissionController {

    @Autowired
    private ILoginUsersPermissionService loginUsersPermissionService;

    @PutMapping("/update")
    @ApiOperation("修改")
    public CommonResult update(@RequestBody  LoginUsersPermissionQO loginUsersPermissionQO,HttpServletRequest request){
        LoginUsersPermission loginUsersPermission = new LoginUsersPermission();
        BeanUtils.copyProperties(loginUsersPermissionQO,loginUsersPermission);
        return loginUsersPermissionService.renewal(loginUsersPermission,request);
    }
    @PostMapping("/save")
    @ApiOperation("添加")
    public CommonResult save(@RequestBody  LoginUsersPermissionQO loginUsersPermissionQO,HttpServletRequest request){
        LoginUsersPermission loginUsersPermission = new LoginUsersPermission();
        BeanUtils.copyProperties(loginUsersPermissionQO,loginUsersPermission);
        return loginUsersPermissionService.create(loginUsersPermission,request);
    }

    @ApiOperation("查找所有菜单")
    @GetMapping(value = "/find")
    public CommonResult find(){
        List<LoginUsersPermissionVO> list= loginUsersPermissionService.find("",0);
        try {
            //配置
            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            treeNodeConfig.setIdKey("pId");
            treeNodeConfig.setNameKey("pName");
            treeNodeConfig.setDeep(5);
            List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig, (treeNode, tree) -> {
                        tree.setId(treeNode.getPId());
                        tree.setParentId(treeNode.getPPId());
                        tree.setName(treeNode.getPName());
                        tree.putExtra("pIcon", treeNode.getPIcon());
                        tree.putExtra("pOrder", treeNode.getPOrder());
                        tree.putExtra("pType", treeNode.getPType());
                        tree.putExtra("pUrl", treeNode.getPUrl());
                        tree.putExtra("pBgcolor", treeNode.getPBgcolor());
                    });
            return CommonResult.success(treeNodes);
        } catch (Exception e) {
            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }
    }

}


